/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Splash.java
 *
 * Created on 7 juil. 2011, 08:54:20
 */
package app.views;

import app.App;
import app.Tools;
import app.core.UpdatedComp;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JRootPane;

/**
 *
 * @author PREVROMA
 */
public class Splash extends javax.swing.JFrame implements UpdatedComp {

    private Image backgroundImage;
    String text;

    /** Creates new form Splash */
    public Splash() {
        initComponents();

        //Remove
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        //Centering
        Toolkit kit = getToolkit();
        int x, y, width, height;
        Dimension screenSize = kit.getScreenSize();
        Insets insets = kit.getScreenInsets(getGraphicsConfiguration());
        width = getWidth();
        height = getHeight();

        x = (screenSize.width - insets.left - insets.right - width) / 2;
        y = (screenSize.height - insets.top - insets.bottom - height) / 2;

        backgroundImage = App.createImage("resources/splash.png").getScaledInstance(width, height, Image.SCALE_SMOOTH);

        text = "Bienvenue sur SyVotes!";

        this.setBounds(x, y, width, height);
        setVisible(true);
    }

    public void paint(Graphics g) {
        g.drawImage(backgroundImage, 0, 0, this);
        g.setFont(new Font("verdana", Font.PLAIN, 12));
        g.setColor(Color.BLACK);
        g.drawString(text, 10, 100);
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(0, 0, getWidth()-2, getHeight()-2);
        pbLoading.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pbLoading = new javax.swing.JProgressBar();
        lblLoading = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        pbLoading.setIndeterminate(true);

        lblLoading.setText("Chargement de BornToStock ...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblLoading, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(pbLoading, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(83, Short.MAX_VALUE)
                .addComponent(lblLoading)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pbLoading, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Splash().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblLoading;
    private javax.swing.JProgressBar pbLoading;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setText(String txt) {
        text = txt;
        repaint();
    }

    @Override
    public void setMax(int i) {
        if (i < 0) { //indeterminate
            pbLoading.setIndeterminate(true);
        } else {
            pbLoading.setIndeterminate(false);
            pbLoading.setMaximum(i);
        }
    }

    @Override
    public void setCurrent(int i) {
        pbLoading.setValue(i);
    }
}
